<template>
  <div :style="background" class="bgBackground">
    <el-form :rules="rules"  label-position="labelPosition" label-width="150px" ref="form" :model="loginForm" class="loginContainer">
      <h2 class="loginTitle">微服务监控度量分析系统</h2>
      <el-form-item label="管理员账号" prop="username">
        <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="管理员密码" prop="password">
        <el-input type="password" v-model="loginForm.password" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" style="width: 100%">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "login",
  data() {
    return {
      background:{

        backgroundImage: 'url(' + require ('../../static/img/background.jpg') + ')',
        backgroundRepeat: 'no-repeat',
        backgroundSize: '100% 100%'
      },
      labelPosition: 'left',
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [{required:true, message:'请输入用户名', trigger:'blur'}],
        password: [{required:true, message:'请输入密码', trigger:'blur'}],
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //连后端Login,controller接口,传3个参数过去,返回token,若成功响应，返回身份数据
          this.getRequest('http://localhost:8003/login',this.loginForm).then(res => {
            if(res)
            {
              //存储用户token和当前登录用户信息
              console.log(res);
              let token = JSON.stringify(res.token);
              console.log(token);
              window.localStorage.setItem("token",token);
              let user = {
                username: res.username,
                password: res.password,
                phone: res.phone
              }
              window.sessionStorage.setItem("user",JSON.stringify(user));
              console.log("登录成功")
              this.$router.push("/home");
            }
          });
        } else {
          this.$message.error('信息错误，请重新输入')
          return false;
        }
      })
    },
    check()
    {
      this.getRequest('http://localhost:8003/checkToken').then(res => {
         console.log(res);
      })
    }
  }
}

</script>

<style>
.loginContainer{
  border-radius: 15px;
  margin: 100px auto;
  width: 400px;
  padding: 15px 35px 15px 35px;
  background: aliceblue;
  border: 1px solid #aaaaaa;
  box-shadow: 0 0 25px #cacaca;

}
.loginTitle{
  margin: 8px auto 40px auto;
  text-align: center;
}
.bgBackground{
  width: 100%;
  height:100%;
  position: fixed;
}
</style>
